package io.renren.modules.qrcode.controller;

import io.renren.common.utils.QRCodeUtil;
import io.renren.common.utils.R;
import io.renren.common.utils.RedisOperator;
import io.renren.common.utils.RedisUtils;
import io.renren.modules.car.service.CarService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;

/**
 * @author zyzhao
 * @date 2022年04月20日 23:02
 */

@Slf4j
@Controller
@RequestMapping("/qrcode")
public class QrcodeController {
    
    private final RedisOperator redisUtils;
    
    private final CarService carService;
    
    @Autowired
    public QrcodeController(RedisOperator redisUtils, CarService carService) {
        this.redisUtils = redisUtils;
        this.carService = carService;
    }
    
    /**
     * @description 二维码页面
     * @return  java.lang.String
     **/
    @GetMapping({"/","/index"})
    public  String index(ModelMap modelMap){
        modelMap.put("userId", 1);
        return "index";
    }
    
    /**
     * @description 生成二维码
     * @param  request
     * @Param  response
     * @return  void
     **/
    @GetMapping("/getQrcode/{url}")
    @ResponseBody
    public void getQrcode(HttpServletRequest request, HttpServletResponse response) throws Exception {
        
        log.info(request.getRequestURL().toString());
    
        String url = request.getRequestURI().split("/")[4].replaceAll("_", "/");
        
    
        //二维码中的链接，需要公网网址才可以用手机扫描出来，本地测试开通natapp进行内网渗透
        //String url = "http://8.130.97.3:8080/renren-fast/swagger-ui.html#/26816204622044924687";
        //String url = "http://192.168.1.11:8080/renren-fast/qrcode/transfer/1";
        //String url = "http://192.168.1.11:8001/#/login";
        //过期时间，30s
        long expireTime = 30;
        //设置参数
        String random = request.getParameter("random");
        String userId = request.getParameter("userId");
        //生成二维码唯一标识
        String key = String.valueOf(System.currentTimeMillis());
        //设置二维码过期时间
        //redisUtils.set(key,random,expireTime);
        //二维码中的内容
        //String content = url + "?key=" + key + "&userId=" + userId;
        String content = url;
        //二维码图片中间logo
        String imgPath = null;
        Boolean needCompress = true;
        //拿到图片流
        ByteArrayOutputStream out = QRCodeUtil.encodeIO("http://" + content, imgPath, needCompress);
        log.info("url为：" + "http://" + content);
        //返回二维码
        response.setCharacterEncoding("UTF-8");
        response.setContentType("image/jpeg;charset=UTF-8");
        response.setContentLength(out.size());
        ServletOutputStream outputStream = response.getOutputStream();
        outputStream.write(out.toByteArray());
        outputStream.flush();
        outputStream.close();
    }
    
    
    @ApiOperation("做中转")
    @GetMapping("/transfer/{carId}")
    public R transferCar(@ApiParam("车辆id") @PathVariable Integer carId){
        return carService.queryCarInfoByCarId(carId);
    }
    
    /**
     * @description 扫描二维码
     * @param  request
     * @Param  key
     * @Param  userId
     * @return  java.lang.String
     **/
    @GetMapping("/scanQrcode")
    @ResponseBody
    public String scanQrcode(HttpServletRequest request, String key, String userId) throws Exception {
        //if(redisUtils.(key)){
        //    redisUtils.set(userId + "_qrcode_status", "success");
        //    return "扫描成功";
        //}
        return "二维码失效, 请重新扫描";
    }
    
    /**
     * @description 验证扫描二维码
     * @param  userId
     **/
    //@RequestMapping("/confirmQrcode")
    //@ResponseBody
    //public AjaxResult confirmQrcode(String userId){
    //    if(redisUtils.exists(userId + "_qrcode_status")){
    //        redisUtils.remove(userId + "_qrcode_status");
    //        //扫描成功后跳转新链接
    //        return AjaxResult.success("扫描成功", "/success");
    //    }
    //    return AjaxResult.error("二维码失效, 请重新扫描");
    //}
    
    /**
     * @description 扫描二维码成功跳转页面
     * @param
     * @return  java.lang.String
     **/
    @GetMapping("/success")
    public  String success(ModelMap modelMap){
        return "success";
    }
    
}
